package com.example.lp.basemvp.presebter;

import android.util.Log;

import com.example.lp.basemvp.baseview.IBaseView;
import com.example.lp.basemvp.baseview.IStartView;
import com.example.lp.basemvp.model.IBaseModel;

import java.lang.ref.WeakReference;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class BasePrsenter<T extends IBaseView, K extends IBaseModel> implements IBasePresenter {
    private static final String TAG = "BasePrsenter";
    protected WeakReference<T> mView;
    protected K mModel;
    public BasePrsenter() {
        try {
        //通过反射来创建 Model
        Type[] types=((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments();
            mModel= (K) ((Class)types[1]).newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttachPresenter(IBaseView view) {
        Log.e(TAG, "onAttachPresenter: ");
        mView = new WeakReference(view);

    }

    @Override
    public void onDetachPresenter() {
        Log.e(TAG, "onDetachPresenter: ");
        if (mView == null) {
            return;
        }
        mView.clear();
    }
}
